package com.example.android.commitcontent.ime.activities;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.android.commitcontent.ime.R;
import com.example.android.commitcontent.ime.cores.base.BaseActivity;
import com.example.android.commitcontent.ime.cores.utils.PopupHelper;
import com.example.android.commitcontent.ime.cores.utils.ToastHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.pluss.baselibrary.http.HttpRequest;
import cn.pluss.baselibrary.http.callback.SimpleHttpCallBack;
import cn.pluss.baselibrary.http.exception.ApiException;

public class FeedbackActivity extends BaseActivity {

    @BindView(R.id.et_name)
    EditText mEtName;
    @BindView(R.id.tv_gender)
    TextView mTvGender;
    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_feedback)
    EditText mEtFeedback;
    @BindView(R.id.tv_text_num)
    TextView mTvTextNum;

    // <0 no select, 0 male , 1 female
    private int mGender = -1;
    private List<String> mGenderList = new ArrayList<>();

    {
        mGenderList.add("男");
        mGenderList.add("女");
    }

    public static void start(Context context) {
        context.startActivity(new Intent(context, FeedbackActivity.class));
    }

    public static void startWithNewTask(Context context) {
        Intent intent = new Intent(context, FeedbackActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected int setLayoutRes() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initView() {
        setTitleBarText("意见反馈");
        hideTitleMoreButton();
        mEtFeedback.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mTvTextNum.setText(String.format("%s个字", mEtFeedback.length()));
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tv_gender)
    void tv_gender(View view) {
        PopupHelper.newInstance(this)
                .setTitleText("请选择您的性别")
                .setPopupWidth(view.getWidth(), false)
                .setDataList(mGenderList, new PopupHelper.DataConverter<String>() {
                    @Override
                    public String convertToString(String data) {
                        return data;
                    }
                })
                .setItemListener(new PopupHelper.OnItemClickListener() {
                    @Override
                    public void onItemClick(PopupWindow popupWindow, int position) {
                        popupWindow.dismiss();
                        mGender = position;
                        mTvGender.setText(position == 0 ? "男" : "女");
                    }
                }).showAsDropDown(view, 0, -6, true);
    }


    @OnClick(R.id.btn_commit)
    void btn_commit() {
        if (mEtName.length() == 0) {
            ToastHelper.show(this, "请输入姓名");
            return;
        }
        if (mGender < 0) {
            ToastHelper.show(this, "请选择性别");
            return;
        }
        if (mEtPhone.length() == 0) {
            ToastHelper.show(this, "请输入手机号");
            return;
        }
        if (mEtFeedback.length() == 0) {
            ToastHelper.show(this, "请输入反馈信息");
            return;
        }
        sendFeedBack();
    }

    public void sendFeedBack() {
        HttpRequest.post("saveSuggestion")
                .params("name", mEtName.getText().toString())
                .params("phone", mEtPhone.getText().toString())
                .params("sex", mTvGender.getText().toString())
                .params("content", mEtFeedback.getText().toString())
                .bindLifecycle(this)
                .execute(new SimpleHttpCallBack<String>() {

                    @Override
                    public void onStart() {
                        showLoadingDialog("请稍等", "提交中..");
                    }

                    @Override
                    public void onSuccess(String string) {
                        ToastHelper.show(FeedbackActivity.this, "提交成功");
                        finish();
                    }

                    @Override
                    public void onError(ApiException e) {
                        dismissLoadingDialog();
                        ToastHelper.show(FeedbackActivity.this, e.getMessage());
                    }

                    @Override
                    public void onCompleted() {
                        dismissLoadingDialog();
                    }
                });
    }
}
